package hotel.service.impl;

import java.util.Date;

import org.springframework.stereotype.Component;

import hotel.entity.Reserve;
import hotel.service.IReserveService;

@Component
public class ReserveServiceImpl<T extends Reserve> extends ServiceImpl<T> implements IReserveService<T> {

	@Override
	public boolean save(T reserve) {
		//根据预计入住时间和预计退房时间计算出持续的天数
		long time=reserve.getEnd_time().getTime()-reserve.getStart_time().getTime();
		int days=(int) Math.ceil(time/1000/60/60/24);
		reserve.setDays(days);
		reserve.setState("已预定");
		reserve.setReserveTime(new Date());
		return dao.save(reserve);
	}

	@Override
	public int getReserveCount() {
		return Integer.parseInt(dao.uniqueResult("select count(*) from Reserve r where r.state ='已预定'"));
	}

	@Override
	public boolean updateReserveState(int reserveid) {
		return dao.executeUpdate("update Reserve r set r.state = '已入住' where r.reserveid = "+reserveid);
	}
}
